package com.ruoyi.web.controller.jiaofu;

import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jiaofu.domain.DdtJiaofuGoods;
import com.ruoyi.jiaofu.service.DdtJiaofuGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品基本信息Controller
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/jiaofu/goods")
public class DdtJiaofuGoodsController extends BaseController {
    private final DdtJiaofuGoodsService ddtJiaofuGoodsService;

    /**
     * 查询商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(DdtJiaofuGoods ddtJiaofuGoods) {
        startPage();
        List<DdtJiaofuGoods> list = ddtJiaofuGoodsService.selectDdtJiaofuGoodsList(ddtJiaofuGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "商品基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DdtJiaofuGoods ddtJiaofuGoods) throws IOException {
        List<DdtJiaofuGoods> list = ddtJiaofuGoodsService.selectDdtJiaofuGoodsList(ddtJiaofuGoods);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "商品基本信息数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DdtJiaofuGoods.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取商品基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ddtJiaofuGoodsService.selectDdtJiaofuGoodsById(id));
    }
}
